<?php

/**
 *    Desc
 *
 *    @author    bansen 2014-05-23
 *    @usage    none
 */
class SellerApp extends StoreadminbaseApp
{
    var $_feed_enabled = false;
    var $_week = array(1=>'周一',2=>'周二',3=>'周三',4=>'周四',5=>'周五',6=>'周六',7=>'周天');
    
    function __construct()
    {
        $this->SellerApp();
    }
    function SellerApp()
    {
        parent::__construct();
       
    }
    function index()
    {
        /* 清除新短消息缓存 */
        $cache_server =& cache_server();
        $cache_server->delete('new_pm_of_user_' . $this->visitor->get('user_id'));

        $user = $this->visitor->get();
        $user_mod =& m('member');
        $info = $user_mod->get_info($user['user_id']);
        $user['portrait'] = portrait($user['user_id'], $info['portrait'], 'middle');
        $this->assign('user', $user);
        
        //商城公告
         $article_mod = &m('article');
         $gonggao  = $article_mod->find(array(
            'conditions'  => 'cate_id = 2 AND store_id=0 AND if_show=1',
            'limit'   => 8,
            'order'   => 'add_time desc'
        )); //找出商城公告符合条件的文章

        /* 店铺信用和好评率 */
        if ($user['has_store'])
        {
            $store_mod =& m('store');
            $store = $store_mod->get_info($user['has_store']);
            $step = intval(Conf::get('upgrade_required'));
            $step < 1 && $step = 5;
            $store['credit_image'] = $this->_view->res_base . '/images/' . $store_mod->compute_credit($store['credit_value'], $step);
            $this->assign('store', $store);
            $this->assign('store_closed', STORE_CLOSED);
        }
        $goodsqa_mod = & m('goodsqa');
        $groupbuy_mod = & m('groupbuy');
        $order_mod =& m('order');
        $goods_mod =& m('goods');
        $returns_mod =& m('returns');


        /* 卖家提醒：待处理订单和待发货订单 */
        if ($user['has_store'])
        {
           $sql1 = "SELECT 
                count(case when status = '" . ORDER_PENDING . "' then order_id end) submitted,
                count(case when status = '" . ORDER_ACCEPTED . "' then order_id end) accepted,
                count(case when status = '" . ORDER_FINISHED . "' then order_id end) finished,
                count(case when status = '" . ORDER_SHIPPED . "' then order_id end) shipped,               
                count(case when status = '" . ORDER_CANCELED . "' then order_id end) canceled
                FROM {$order_mod->table} where seller_id = '{$user['user_id']}'";
           $order_data = $order_mod->getRow($sql1);
           $sql2 = "SELECT 
                count(case when if_show = 1 then goods_id end) reveal,
                count(case when if_show = 0 then goods_id end) hide,        
                count(case when if_show = 2 then goods_id end) closed
                FROM {$goods_mod->table} where store_id = '{$user['user_id']}'";
           $goods_data = $order_mod->getRow($sql2);
            $sql9 = "SELECT COUNT(*) FROM {$goodsqa_mod->table} WHERE store_id = '{$user['user_id']}' AND reply_content ='' ";
            $sql10 = "SELECT COUNT(*) FROM {$groupbuy_mod->table} WHERE store_id='{$user['user_id']}' AND state = " .GROUP_END;
            $sql11 = "SELECT COUNT(returns_id) FROM {$returns_mod->table} WHERE seller_id = '{$user['user_id']}' AND status IN (1,3,4,5,6,7)";
            $seller_stat = array(
                'submitted' => $order_data['submitted'],
                'accepted'  => $order_data['accepted'],
                'finished'  => $order_data['finished'],
                'shipped'   => $order_data['shipped'],
                'canceled'  => $order_data['canceled'],
                'show'  => $goods_data['reveal'],
                'hide'  => $goods_data['hide'],
                'closed'  => $goods_data['closed'],
                'replied'   => $goodsqa_mod->getOne($sql9),
                'groupbuy_end'   => $goodsqa_mod->getOne($sql10),
                'returns'   => $returns_mod->getOne($sql11),
            );
            $this->assign('seller_stat', $seller_stat);
        }
        /* 卖家提醒： 店铺等级、有效期、商品数、空间 */
        if ($user['has_store'])
        {
            $store_mod =& m('store');
            $store = $store_mod->get_info($user['has_store']);

            $grade_mod = & m('sgrade');
            $grade = $grade_mod->get_info($store['sgrade']);

            $goods_mod = &m('goods');
            $goods_num = $goods_mod->get_count_of_store($user['has_store']);
            $uploadedfile_mod = &m('uploadedfile');
            $space_num = $uploadedfile_mod->get_file_size($user['has_store']);
            $sgrade = array(
                'grade_name' => $grade['grade_name'],
                'add_time' => empty($store['end_time']) ? 0 : sprintf('%.2f', ($store['end_time'] - gmtime())/86400),
                'goods' => array(
                    'used' => $goods_num,
                    'total' => $grade['goods_limit']),
                'space' => array(
                    'used' => sprintf("%.2f", floatval($space_num)/(1024 * 1024)),
                    'total' => $grade['space_limit']),
                    );
            $this->assign('sgrade', $sgrade);

        }

        /* 待审核提醒 */
        if ($user['state'] != '' && $user['state'] == STORE_APPLYING)
        {
            $this->assign('applying', 1);
        }
        /* 当前位置 */
        $this->_curlocal(LANG::get('seller_center'),    url('app=seller'),
                         LANG::get('overview'));

        /* 当前用户中心菜单 */
        $this->_curitem('overview');
        $this->_config_seo('title', Lang::get('seller_center'));
        $this->assign('security_data', $this->account_security_level());
        $this->assign('safety_level', array('1' => '低','2' => '中','3' => '高'));
        $this->assign('date',date('m-d'));
        $this->assign('week',$this->_week[date('N')]);
        $this->assign('gonggao',$gonggao);
        $this->display('seller.index.html');
    }
    /**
     *账号安全等级
     */
    function account_security_level()
    {
        $user_safety_mod =& m('usersafety');
        $i = 0;
        $rs = array();
        $accout_security = $user_safety_mod->get(array('conditions'    => 'user_id = ' . $this->visitor->get('user_id')));
        if($accout_security['pay_password']){$i++;}
        if($accout_security['binding_email']){$i++;}
        if($accout_security['binding_mobile']){$i++;}
        if($i==0){$i=1;}
        $rs['level'] = $i;
        $rs['security'] = $accout_security;
       return $rs;
               
    }
    

}

?>
